<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
        integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <script src="./js/vue.js"></script>
</head>

<body>

    <div id="app" class="container">
        <form class="form-horizontal">

            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label">书名</label>
                <div class="col-sm-10">
                    <input type="text" v-model="name" class="form-control" id="inputEmail3" placeholder="请输入图书名称">
                </div>
            </div>
        </form>
        <div class="text-center">
            <button class="btn btn-primary" @click="add">添加</button>
        </div>

        <table class="table table-hover table-bordered">
            <thead>
                <tr>
                    <th>id</th>
                    <th>书名</th>
                    <th>添加时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item,index) in arr" :class="[index%2==0?'success':'info']">
                    <td>{{index+1}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.time}}</td>
                    <td>
                        <button class="btn btn-danger" @click="del(index)">删除</button>
                    </td>
                </tr>
                <tr v-if="arr.length===0">
                    <td colspan="5" class="text-center">暂无数据</td>
                </tr>
                <tr v-else>
                    <td colspan="5" class="text-center">
                        <button class="btn btn-danger" @click="delAll">全部删除</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        let vm = new Vue({
            el: "#app",
            data: {
                arr: [
                ],
                name: ""
            },
            methods: {
                add() {
                    this.arr.push({
                        name: this.name,
                        time: new Date().toLocaleDateString()
                    })
                    this.reset()
                },
                del(index) {
                    this.arr.splice(index, 1)
                },
                delAll() {
                    this.arr = []
                }

            }
        })
    </script>
</body>

</html>